use Data/anesTimeSeriesDataSubset.dta, clear

*Number of calls*
tab VCF0004, sum(VCF9124)

recode VCF9124 0=. 99=.
drop if VCF9124==.

gen callPercentile = .
foreach x in 1980 1984 1988 1992 1996 2000 2004 2008 2016 {
xtile temp = VCF9124 if VCF0004==`x', n(100)
replace callPercentile = temp if VCF0004==`x'
drop temp
}

local start = 100
local declineRate = 10
local prop = `start'+`declineRate'
gen cutoff = 0
foreach x in 1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 {
local prop = `prop'-`declineRate'
disp `x' " = " `prop'
replace cutoff = `prop' if VCF0004 == `x'
}

gen callSample = 0
replace callSample = 1 if callPercentile < cutoff

*Binary partisanship*
recode VCF0303 0=. 1=0 2=. 3=1, gen(demRep)


*Government spending*
tab VCF0839
recode VCF0839 0=. 9=.

tab VCF0004, sum(VCF0839)

esize twosample VCF0839  if VCF0004 == 1984 , by(demRep) cohensd
matrix temp = 1 , 1984 , r(d) , r(lb_d) , r(ub_d), r(N_1), r(N_2), 0
matrix eSize = temp


foreach x in 1988 1992 1996 2000 2004 2008 2016 {
esize twosample VCF0839  if VCF0004 == `x' , by(demRep) cohensd
matrix temp = 1 , `x' , r(d) , r(lb_d) , r(ub_d), r(N_1), r(N_2), 0
matrix eSize = eSize \ temp
}

foreach x in 1984 1988 1992 1996 2000 2004 2008 2016 {
esize twosample VCF0839  if VCF0004 == `x' & callSample==1, by(demRep) cohensd
matrix temp = 1 , `x' , r(d) , r(lb_d) , r(ub_d), r(N_1), r(N_2), 1
matrix eSize = eSize \ temp
}


*Abortion*
tab VCF0838
recode  VCF0838 0=. 9=.
tab VCF0004, sum(VCF0838)

foreach x in 1980 1984 1988 1992 1996 2000 2004 2008 2016 {
esize twosample VCF0838  if VCF0004 == `x' , by(demRep) cohensd
matrix temp = 2 , `x' , r(d) , r(lb_d) , r(ub_d), r(N_1), r(N_2), 0
matrix eSize = eSize \ temp
}

foreach x in 1980 1984 1988 1992 1996 2000 2004 2008 2016 {
esize twosample VCF0838  if VCF0004 == `x' & callSample==1, by(demRep) cohensd
matrix temp = 2 , `x' , r(d) , r(lb_d) , r(ub_d), r(N_1), r(N_2), 1
matrix eSize = eSize \ temp
}


*Aid to Blacks*
tab VCF0830
recode VCF0830 0=. 9=.
tab VCF0004, sum(VCF0830)

foreach x in 1980 1984 1988 1992 1996 2000 2004 2008 2016 {
esize twosample VCF0830  if VCF0004 == `x' , by(demRep) cohensd
matrix temp = 3 , `x' , r(d) , r(lb_d) , r(ub_d), r(N_1), r(N_2), 0
matrix eSize = eSize \ temp
}

foreach x in 1980 1984 1988 1992 1996 2000 2004 2008 2016 {
esize twosample VCF0830  if VCF0004 == `x' & callSample==1, by(demRep) cohensd
matrix temp = 3 , `x' , r(d) , r(lb_d) , r(ub_d), r(N_1), r(N_2), 1
matrix eSize = eSize \ temp
}


*Health insurance*
tab VCF0806
recode VCF0806 0=. 9=.
tab VCF0004, sum(VCF0806)

foreach x in 1984 1988 1992 1996 2000 2004 2008 2016 {
esize twosample VCF0806  if VCF0004 == `x' , by(demRep) cohensd
matrix temp = 4 , `x' , r(d) , r(lb_d) , r(ub_d), r(N_1), r(N_2), 0
matrix eSize = eSize \ temp
}

foreach x in 1984 1988 1992 1996 2000 2004 2008 2016 {
esize twosample VCF0806  if VCF0004 == `x' & callSample==1, by(demRep) cohensd
matrix temp = 4 , `x' , r(d) , r(lb_d) , r(ub_d), r(N_1), r(N_2), 1
matrix eSize = eSize \ temp
}

*Defense spending*
tab VCF0843
recode VCF0843 0=. 9=.
tab VCF0004, sum(VCF0843)

foreach x in 1980 1984 1988 1992 1996 2000 2004 2008 2016 {
esize twosample VCF0843  if VCF0004 == `x' , by(demRep) cohensd
matrix temp = 5 , `x' , r(d) , r(lb_d) , r(ub_d), r(N_1), r(N_2), 0
matrix eSize = eSize \ temp
}

foreach x in 1980 1984 1988 1992 1996 2000 2004 2008 2016 {
esize twosample VCF0843  if VCF0004 == `x' & callSample==1, by(demRep) cohensd
matrix temp = 5 , `x' , r(d) , r(lb_d) , r(ub_d), r(N_1), r(N_2), 1
matrix eSize = eSize \ temp
}

*Jobs*
tab VCF0809
recode VCF0809 0=. 9=.
tab VCF0004, sum(VCF0809)

foreach x in 1980 1984 1988 1992 1996 2000 2004 2008 2016 {
esize twosample VCF0809  if VCF0004 == `x' , by(demRep) cohensd
matrix temp = 6 , `x' , r(d) , r(lb_d) , r(ub_d), r(N_1), r(N_2), 0
matrix eSize = eSize \ temp
}

foreach x in 1980 1984 1988 1992 1996 2000 2004 2008 2016 {
esize twosample VCF0809  if VCF0004 == `x' & callSample==1, by(demRep) cohensd
matrix temp = 6 , `x' , r(d) , r(lb_d) , r(ub_d), r(N_1), r(N_2), 1
matrix eSize = eSize \ temp
}


*Plots*
matrix colnames eSize = variable year cohensD lb ub demN repN callsTrim

clear
svmat eSize, names(col) 

set scheme s1mono

foreach y in 3 4 5 6 {
foreach x in cohensD lb ub {
replace `x' = -`x' if variable==`y'
}
}

recode variable 1=2 2=3 3=1, into(label)

label define label ///
1 "Aid to blacks" ///
2 "Government spending" ///
3 "Abortion" ///
4 "Health insurance" ///
5 "Defense  Spending" ///
6 "Jobs" ///
, replace

label values label label

twoway ///
(line cohensD year if callsTrim==0, lcolor(black) lpattern(solid)) ///
(line cohensD year if callsTrim==1, lcolor(gs5) lpattern(dash)) ///
(rarea lb ub year if callsTrim==0, fcolor(gs14%25) lwidth(none)) ///
(rarea lb ub year if callsTrim==1, fcolor(gs12%25) lwidth(none)) ///
, xtitle("") xlabel(1980 1984 1988 1992 1996 2000 2004 2008 2016, labsize(vsmall) alternate) ylabel(,labsize(vsmall) angle(horizontal)) xmtick(1984 1992 2000 2008, ticks tlength(medium))  plotregion(lcolor(none)) ///
by(label, note("") graphregion(margin(zero)) rows(1)) subtitle(, nobox)  xsize(16) ysize(7) yscale(noextend) xscale(noextend) ///
legend(order(1 "Actual ANES sample" 2 "Simulated low response rate ANES sample") rows(1) region(lcolor(none) margin(zero)))  ytitle(Polarization (Cohen's D))

 
graph export Figures/Figure_2.pdf, as(pdf) replace

gen totalN = demN + repN

keep year totalN callsTrim

collapse totalN, by(year callsTrim)

reshape wide totalN, i(year) j(callsTrim)

gen prop = totalN1/totalN0

gen unit = .
replace unit = 71.8 if year == 1980
replace unit = 72.1 if year == 1984
replace unit = 70.5 if year == 1988
replace unit = 74 if year == 1992
replace unit = 59.8 if year == 1996
replace unit = 60.5 if year == 2000
replace unit = 66.1 if year == 2004
replace unit = 63.7 if year == 2008
replace unit = 55.4 if year == 2016

gen simRR = prop*unit

twoway ///
(line unit year, lcolor(black) lpattern(solid)) ///
(line simRR year, lcolor(gs5) lpattern(dash)) ///
, xtitle("") xlabel(1980 1984 1988 1992 1996 2000 2004 2008 2016, labsize(vsmall) alternate) ylabel(,labsize(vsmall) angle(horizontal)) xmtick(1984 1992 2000 2008, ticks tlength(medium))  plotregion(lcolor(none)) ///
 xsize(16) ysize(12) yscale(noextend) xscale(noextend) ///
 legend(order(1 "Actual response rate" 2 "Simulated response rate") rows(1) region(lcolor(none) margin(zero)))  ytitle(%)

  graph export Figures/Figure_A5.pdf, as(pdf) replace
